package hl.demosample.app.util

import android.animation.*
import android.view.View
import android.view.ViewGroup
import android.view.animation.AccelerateDecelerateInterpolator
import hl.demosample.app.weight.jellyInerpolator.JellyInterpolator

/*
* path: DemoSample 
* Author: hl
* Create: 2021-08-18 10:47
* Changes (from 2021-08-18)
* Desc:
*/
object LoginAndRegisterAnimation {
    /** 输入框的动画效果** @param view* 控件* @param w* 宽* @param h* 高*/
    fun inputAnimator(view: View, w: Float, h: Float, inputLayout: View, layoutProgress: View) {
        val set = AnimatorSet()
        val animator: ValueAnimator = ValueAnimator.ofFloat(0F, w)
        animator.addUpdateListener { animation ->
            val value = animation.animatedValue as Float
            val params: ViewGroup.MarginLayoutParams = view
                .layoutParams as ViewGroup.MarginLayoutParams
            params.leftMargin = value.toInt()
            params.rightMargin = value.toInt()
            view.layoutParams = params
        }
        val animator2: ObjectAnimator = ObjectAnimator.ofFloat(inputLayout, "scaleX", 1f, 0.5f)
        set.duration = 1000
        set.interpolator = AccelerateDecelerateInterpolator()
        set.playTogether(animator, animator2)
        set.start()
        set.addListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(animation: Animator?) {}
            override fun onAnimationRepeat(animation: Animator?) {}
            override fun onAnimationEnd(animation: Animator?) {
                /**
                 * 动画结束后，先显示加载的动画，然后再隐藏输入框
                 */
                layoutProgress.visibility = View.VISIBLE
                progressAnimator(layoutProgress)
                inputLayout.visibility = View.INVISIBLE
            }

            override fun onAnimationCancel(animation: Animator?) {

            }
        })
    }

    /*** 出现进度动画** @param view ***/
    fun progressAnimator(view: View) {
        val animator: PropertyValuesHolder = PropertyValuesHolder.ofFloat("scaleX", 0.5f, 1f)
        val animator2: PropertyValuesHolder = PropertyValuesHolder.ofFloat("scaleY", 0.5f, 1f)
        val animator3 = ObjectAnimator.ofPropertyValuesHolder(view, animator, animator2)
        animator3.duration = 1000
        animator3.interpolator = JellyInterpolator()
        animator3.start()
    }

    /*** 恢复初始状态 ***/
    fun recovery(layoutProgress: View, inputLayout: View) {
        layoutProgress.visibility = View.GONE
        inputLayout.visibility = View.VISIBLE
        val params = inputLayout.layoutParams as ViewGroup.MarginLayoutParams
        params.leftMargin = 20
        params.rightMargin = 20
        inputLayout.layoutParams = params
        val animator2 = ObjectAnimator.ofFloat(inputLayout, "scaleX", 0.5f, 1f)
        animator2.duration = 500
        animator2.interpolator = AccelerateDecelerateInterpolator()
        animator2.start()
    }
}